--- title: Title keywords: fastai sidebar: home_sidebar nb_path: "nbs/inference.ipynb" ---
{% raw %}
{% endraw %}

Inference

{% raw %}
%reload_ext autoreload
%autoreload 2
{% endraw %} {% raw %}
from pathlib import Path
from itertools import groupby
import numpy as np
from pycocotools import mask as mutils
from kgl_humanprotein.data import *
{% endraw %} {% raw %}
import holoviews as hv
hv.extension('bokeh')
{% endraw %} {% raw %}
dir_data = Path('../data')
dir_models = Path('../models/')

dir_hpa = dir_data/'hpa-single-cell-image-classification'
dir_test_img = 
{% endraw %}

Generate cell crops

Segment the cells from the test images. Save these crops of cells to disk, keeping track of the image the crops belong to.

{% raw %}
# from 'train' as the test images.
dir_testimg = dir_hpa/'train'
imgids = imgids_testing[:]
{% endraw %}

Create and load CellSegmentor.

{% raw %}
dir_cellseg_model = Path('../models/HPA_Cell_Segmentation/')
NUC_MODEL = dir_cellseg_model/'nuclei-model.pth'
CELL_MODEL = dir_cellseg_model/'cell-model.pth'

segmentator = CellSegmentator(
    NUC_MODEL,
    CELL_MODEL,
    scale_factor=0.25,
    device="cuda",
    padding=False,
    multi_channel_model=True)
No GPU found, using CPU.
/Users/jack/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'pytorch_zoo.unet.DPNUnet' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/Users/jack/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.container.ModuleList' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/Users/jack/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.container.Sequential' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/Users/jack/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.conv.Conv2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/Users/jack/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.activation.ReLU' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/Users/jack/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.upsampling.Upsample' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/Users/jack/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.batchnorm.BatchNorm2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
/Users/jack/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/serialization.py:649: SourceChangeWarning: source code of class 'torch.nn.modules.pooling.MaxPool2d' has changed. you can retrieve the original source code by accessing the object's source attribute or set `torch.nn.Module.dump_patches = True` and use the patch tool to revert the changes.
  warnings.warn(msg, SourceChangeWarning)
{% endraw %} {% raw %}
fns_red, fns_yellow, fns_blue = (
    [dir_hpa/'train'/f'{id}_{color}.png' for id in imgids]
    for color in ('red', 'yellow', 'blue'))
{% endraw %} {% raw %}
masks = segmentator(fns_red, fns_yellow, fns_blue)
Predicting nuclei and cells...
0it [00:00, ?it/s]
done
Labelling cells...
1it [00:05,  5.06s/it]
{% endraw %} {% raw %}
_, mask = masks[0]
{% endraw %} {% raw %}
img = load_RGBY_image(dir_hpa, imgid, )
{% endraw %} {% raw %}
fig = hv.Image(img[...,1]) + hv.RGB(img)
fig.options({'Image': dict(cmap='kgy', colorbar=True)})
{% endraw %}